package main

//导入以前若干天之前的所有数据到自己库

import (
	"encoding/json"
	"github.com/gocolly/colly/v2"
	"io/ioutil"
	"log"
	"time"
)

type CarSubscribe struct {
	CarNumber          string `json:"carNumber"`
	SubscribeTime      string `json:"subscribeTime"`
	SubscribeCode      string `json:"subscribeCode"`
	SubscribeName      string `json:"subscribeName"`
	SubscribeTimePhase string `json:"subscribeTimePhase"`
	ImportFlag         string `json:"importFlag"`
	SubscribeStatus    string `json:"subscribeStatus"`
	SysUserId          string `json:"sysUserId"`
	SysNickName        string `json:"sysNickName"`
}

var (
	perTimeCount = "1000"
)

func main() {
	i := -1
	for {

		nowTime := time.Now().AddDate(0, 0, i).Format("2006-01-02")
		if nowTime == "2020-04-06" { //截止2020 4 6
			break
		}
		log.Println("采集时间" + nowTime + "每次采集数量" + perTimeCount)
		threadCollect(nowTime)
		i--
	}

}

func threadCollect(nowTime string) {
	defer func() {
		if err := recover(); err != nil {
			log.Println("下面异常抛出来了", err)
			return
		}
	}()

	content, err := ioutil.ReadFile("cookie.txt")
	if err != nil {
		log.Println(err)
		return
	}
	//fmt.Println("读取到的cookie:" + string(content))
	resultC := colly.NewCollector()
	resultC.OnRequest(func(r *colly.Request) {
		r.Headers.Set("Content-Type", "application/json")
	})
	resultC.OnResponse(func(r *colly.Response) {
		log.Println("导入到服务器结果：", string(r.Body))
	})
	resultC.OnError(func(r *colly.Response, err error) {
		log.Println(r.Request.URL, "连接不到服务", err)
	})
	c := colly.NewCollector(
		colly.UserAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"),
	)

	c.OnRequest(func(r *colly.Request) {
		r.Headers.Set("Content-Type", "application/x-www-form-urlencoded")
		r.Headers.Set("Cookie", string(content))

	})
	c.OnError(func(r *colly.Response, err error) {
		log.Println("请求12123异常:", r.Request.URL, "failed with response:", r, "\nError:", err)
		return
	})

	var sliceCar []CarSubscribe
	c.OnHTML("tbody tr", func(e *colly.HTMLElement) {
		carSubscribe := CarSubscribe{}
		carSubscribe.SubscribeName = e.ChildText("td:nth-child(1)")
		carSubscribe.CarNumber = e.ChildText("td:nth-child(3)")
		carSubscribe.SubscribeTime = e.ChildText("td:nth-child(4)")
		carSubscribe.SubscribeTimePhase = e.ChildText("td:nth-child(5)")
		carSubscribe.SubscribeCode = e.ChildText("td:nth-child(6)")
		status := "0"
		if e.ChildText("td:nth-child(7)") == "取消" {
			status = "1"
		}
		carSubscribe.SubscribeStatus = status
		carSubscribe.SysUserId = "1"
		carSubscribe.SysNickName = "admin"
		carSubscribe.ImportFlag = "1"
		sliceCar = append(sliceCar, carSubscribe)
		//exception1 := e.ChildText("td:nth-child(8)")
		//log.Println(exception1[3])

	})
	err = c.Post("https://bj.122.gov.cn/veh/jcz/yyxx/list", map[string]string{"ksrq": nowTime,
		"jsrq": nowTime, "page": "1", "size": perTimeCount})
	if err != nil {
		log.Println(err)
		return
	}
	if len(sliceCar) == 0 {
		log.Println("没有采集到，再采集一次?")
		return
	}
	log.Println("采集到所有对象数量", len(sliceCar))
	r, _ := json.Marshal(sliceCar)
	resultC.PostRaw("https://car.songlingyc.net/carcheck/carnumber/importJiaoguan", r)

}
